﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace GiangDay
{
    public partial class FrGiangdayEdit : DevComponents.DotNetBar.OfficeForm
    {
        FunctionDung fun = new FunctionDung();
        public FrGiangday giangday = new FrGiangday();
        string mon_ht = "";
        string lop_ht = "";
        string khoa_ht = "";
        string hocki_ht = "";

        public FrGiangdayEdit()
        {
            InitializeComponent();
            //Load Khóa
            cbKhoa.DataSource=fun.loadKhoa();
            cbKhoa.ValueMember = "MA_KHOA";
            cbKhoa.DisplayMember = "DKHOA";

            //Load Lớp
            cbLop.DataSource = fun.loadLop();
            cbLop.ValueMember = "MA_LOP";
            cbLop.DisplayMember = "TEN_LOP";         
            
            //Load Giáo viên
            cbGiaovien.DataSource = fun.loadGiaovien();
            cbGiaovien.ValueMember = "MA_GV";
            cbGiaovien.DisplayMember = "TEN_GV";
            
            //Load Môn học
            cbMonhoc.DataSource = fun.loadMonhoc();
            cbMonhoc.ValueMember = "MA_MON";
            cbMonhoc.DisplayMember = "TEN_MON";
        }
        public void setInitCB(string lopht, string monht, string khoaht, string hockiht, string giaovienht)
        {
            cbLop.SelectedValue = lopht;
            cbMonhoc.SelectedValue = monht;
            cbKhoa.SelectedValue = khoaht;
            cbHocky.SelectedValue = hockiht;
            cbGiaovien.SelectedValue = giaovienht;
            cbLop.Enabled = false;
            cbMonhoc.Enabled = false;
            mon_ht = monht;
            lop_ht = lopht;
            khoa_ht = khoaht;
            hocki_ht = hockiht;
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_molop_Click(object sender, EventArgs e)
        {
            lb_thongbao.Text = "";
            if (cbKhoa.SelectedIndex == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                lb_thongbao.Text = "Lỗi: Chưa chọn khóa .";
                cbKhoa.Focus();
                return;
            }
            if (cbHocky.SelectedIndex == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                lb_thongbao.Text = "Lỗi: Chưa chọn học kỳ .";
                cbHocky.Focus();
                return;
            }
            if (cbLop.SelectedIndex == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                lb_thongbao.Text = "Lỗi: Chưa chọn lớp .";
                cbLop.Focus();
                return;
            }
            if (cbMonhoc.SelectedIndex == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                lb_thongbao.Text = "Lỗi: Chưa chọn môn học .";
                cbMonhoc.Focus();
                return;
            }
            if (cbGiaovien.SelectedIndex == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                lb_thongbao.Text = "Lỗi: Chưa chọn giáo viên .";
                cbGiaovien.Focus();
                return;
            }
            if (cbMonhoc.SelectedValue.ToString() == mon_ht)
            {
                fun.xoaGiangday(khoa_ht,
                                hocki_ht,
                                lop_ht,
                                mon_ht,
                                0);
                fun.themMolop( cbMonhoc.SelectedValue.ToString(),
                               cbKhoa.SelectedValue.ToString(), 
                               cbHocky.SelectedValue.ToString(),
                               cbLop.SelectedValue.ToString(),
                               cbGiaovien.SelectedValue.ToString());
                giangday.loadGrid();               
                this.Close();
            }
            if (fun.tontaiLop(cbMonhoc.SelectedValue.ToString(), cbKhoa.SelectedValue.ToString(), cbLop.SelectedValue.ToString(), cbHocky.SelectedValue.ToString(), 0))
            {
                System.Media.SystemSounds.Beep.Play();
                lb_thongbao.Text = "Lỗi: Môn đã tồn tại";
                return;
            }
            fun.xoaGiangday(khoa_ht,
                                hocki_ht,
                                lop_ht,
                                mon_ht,
                                0);
            fun.themMolop(cbMonhoc.SelectedValue.ToString(),
                           cbKhoa.SelectedValue.ToString(),
                           cbHocky.SelectedValue.ToString(),
                           cbLop.SelectedValue.ToString(),
                           cbGiaovien.SelectedValue.ToString());
            giangday.loadGrid();
            //this.Close();
        }

        private void cbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Load Học kì
            cbHocky.DataSource = fun.loadHocky(cbKhoa.SelectedValue.ToString());
            cbHocky.ValueMember = "MA_HK";
            cbHocky.DisplayMember = "TEN_HK";
        }
             
       
    }
}